package com.campus.client.view;

import com.campus.client.controller.StudentMainController;
import com.campus.client.controller.TeacherMainController;
import com.campus.client.net.ClientConnector;
import com.campus.common.vo.Student;
import com.campus.common.vo.User;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.io.IOException;

public class StudentMainFrame {

    Button changePasswordBtn = new Button("修改密码");
    Button logoutBtn = new Button("退出登录");
    Button bankBtn = new Button("校园银行");
    Button studentInfoBtn = new Button("我的学籍");
    Button libraryBtn = new Button("图书馆系统");
    Button storeBtn = new Button("商店系统");
    Button courseSelectionBtn = new Button("选课系统");

    private User user=new User();
    public User getUser() {return user;}
    public void setUser(String Id,String password,String role) {
        this.user.setId(Id);
        this.user.setPassword(password);
        this.user.setRole(role);
        System.out.print("用户名"+this.user.getId());
    }

    private StudentMainController controller;
    private ClientConnector connector;


    public void show(Stage primaryStage) throws IOException {
        // 创建主VBox容器
        VBox mainVBox = new VBox();

        connector = new ClientConnector("172.20.10.2", 8888);
        controller = new StudentMainController(this, connector);

        mainVBox.setPrefHeight(500.0);
        mainVBox.setPrefWidth(802.0);

        // 创建AnchorPane
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setMaxHeight(Double.NEGATIVE_INFINITY);
        anchorPane.setMaxWidth(Double.NEGATIVE_INFINITY);
        anchorPane.setPrefHeight(500.0);
        anchorPane.setPrefWidth(802.0);
        AnchorPane.setTopAnchor(anchorPane, 0.0);
        AnchorPane.setBottomAnchor(anchorPane, 0.0);
        AnchorPane.setLeftAnchor(anchorPane, 0.0);
        AnchorPane.setRightAnchor(anchorPane, 0.0);

        // 创建背景图片
        ImageView backgroundImage = new ImageView();
        try {
            Image bgImage = new Image("background.png");
            backgroundImage.setImage(bgImage);
        } catch (Exception e) {
            System.out.println("无法加载背景图片: " + e.getMessage());
        }
        backgroundImage.setFitHeight(500.0);
        backgroundImage.setFitWidth(802.0);
        backgroundImage.setLayoutY(55.0);
        backgroundImage.setPreserveRatio(true);
        backgroundImage.setPickOnBounds(true);

        // 创建左侧半透明矩形
        Rectangle leftRect = new Rectangle();
        leftRect.setArcHeight(5.0);
        leftRect.setArcWidth(5.0);
        leftRect.setFill(Color.web("#d5e6f6"));
        leftRect.setHeight(432.0);
        leftRect.setLayoutY(68.0);
        leftRect.setOpacity(0.51);
        leftRect.setStroke(Color.BLACK);
        leftRect.setStrokeType(javafx.scene.shape.StrokeType.INSIDE);
        leftRect.setWidth(233.0);

        // 创建顶部蓝色矩形
        Rectangle topRect = new Rectangle();
        topRect.setArcHeight(5.0);
        topRect.setArcWidth(5.0);
        topRect.setFill(Color.web("#044684"));
        topRect.setHeight(68.0);
        topRect.setStroke(Color.BLACK);
        topRect.setStrokeType(javafx.scene.shape.StrokeType.INSIDE);
        topRect.setWidth(802.0);

        // 创建标题文本
        Text titleText = new Text("虚拟校园系统");
        titleText.setFill(Color.WHITE);
        titleText.setLayoutX(321.0);
        titleText.setLayoutY(46.0);
        titleText.setStrokeWidth(0.0);
        titleText.setTextAlignment(javafx.scene.text.TextAlignment.CENTER);
        titleText.setFont(Font.font("System", FontWeight.BOLD, 30.0));

        // 创建修改密码按钮
        changePasswordBtn.setLayoutX(20.0);
        changePasswordBtn.setLayoutY(460.0);
        changePasswordBtn.setMnemonicParsing(false);

        // 创建退出登录按钮
        logoutBtn.setLayoutX(155.0);
        logoutBtn.setLayoutY(460.0);
        logoutBtn.setMnemonicParsing(false);

        // 创建校园银行HBox
        HBox bankHBox = new HBox();
        bankHBox.setAlignment(Pos.CENTER);
        bankHBox.setLayoutX(1.0);
        bankHBox.setLayoutY(352.0);
        bankHBox.setPrefHeight(42.0);
        bankHBox.setPrefWidth(231.0);
        bankHBox.setSpacing(10.0);

        // 校园银行图标
        ImageView bankIcon = new ImageView();
        try {
            Image bankImage = new Image("bank.png");
            bankIcon.setImage(bankImage);
        } catch (Exception e) {
            System.out.println("无法加载校园银行图标: " + e.getMessage());
        }
        bankIcon.setFitHeight(58.0);
        bankIcon.setFitWidth(66.0);
        bankIcon.setPreserveRatio(true);
        bankIcon.setPickOnBounds(true);

        // 校园银行按钮
        bankBtn.setAlignment(Pos.CENTER);
        bankBtn.setMnemonicParsing(false);
        bankBtn.setPrefHeight(42.0);
        bankBtn.setPrefWidth(135.0);
        bankBtn.setStyle("-fx-background-color: #1E90FF;");
        bankBtn.setTextFill(Color.WHITE);
        bankBtn.setFont(Font.font("System", FontWeight.BOLD, 18.0));

        bankHBox.getChildren().addAll(bankIcon, bankBtn);

        // 创建我的学籍HBox
        HBox studentInfoHBox = new HBox();
        studentInfoHBox.setAlignment(Pos.CENTER);
        studentInfoHBox.setLayoutY(87.0);
        studentInfoHBox.setPrefHeight(48.0);
        studentInfoHBox.setPrefWidth(233.0);
        studentInfoHBox.setSpacing(10.0);

        // 我的学籍图标
        ImageView studentInfoIcon = new ImageView();
        try {
            Image studentInfoImage = new Image("info.png");
            studentInfoIcon.setImage(studentInfoImage);
        } catch (Exception e) {
            System.out.println("无法加载我的学籍图标: " + e.getMessage());
        }
        studentInfoIcon.setFitHeight(58.0);
        studentInfoIcon.setFitWidth(66.0);
        studentInfoIcon.setPreserveRatio(true);
        studentInfoIcon.setPickOnBounds(true);

        // 我的学籍按钮
        studentInfoBtn.setAlignment(Pos.CENTER);
        studentInfoBtn.setMnemonicParsing(false);
        studentInfoBtn.setPrefHeight(42.0);
        studentInfoBtn.setPrefWidth(135.0);
        studentInfoBtn.setStyle("-fx-background-color: #1E90FF;");
        studentInfoBtn.setTextFill(Color.WHITE);
        studentInfoBtn.setFont(Font.font("System", FontWeight.BOLD, 18.0));

        studentInfoHBox.getChildren().addAll(studentInfoIcon, studentInfoBtn);

        // 创建选课系统HBox
        HBox courseSelectionHBox = new HBox();
        courseSelectionHBox.setAlignment(Pos.CENTER);
        courseSelectionHBox.setLayoutX(1.0);
        courseSelectionHBox.setLayoutY(152.0);
        courseSelectionHBox.setPrefHeight(42.0);
        courseSelectionHBox.setPrefWidth(231.0);
        courseSelectionHBox.setSpacing(10.0);

        // 选课系统图标
        ImageView courseSelectionIcon = new ImageView();
        try {
            Image courseSelectionImage = new Image("course.png");
            courseSelectionIcon.setImage(courseSelectionImage);
        } catch (Exception e) {
            System.out.println("无法加载选课系统图标: " + e.getMessage());
        }
        courseSelectionIcon.setFitHeight(58.0);
        courseSelectionIcon.setFitWidth(66.0);
        courseSelectionIcon.setPreserveRatio(true);
        courseSelectionIcon.setPickOnBounds(true);

        // 选课系统按钮
        courseSelectionBtn.setAlignment(Pos.CENTER);
        courseSelectionBtn.setMnemonicParsing(false);
        courseSelectionBtn.setPrefHeight(42.0);
        courseSelectionBtn.setPrefWidth(135.0);
        courseSelectionBtn.setStyle("-fx-background-color: #1E90FF;");
        courseSelectionBtn.setTextFill(Color.WHITE);
        courseSelectionBtn.setFont(Font.font("System", FontWeight.BOLD, 18.0));

        courseSelectionHBox.getChildren().addAll(courseSelectionIcon, courseSelectionBtn);

        // 创建图书馆系统HBox
        HBox libraryHBox = new HBox();
        libraryHBox.setAlignment(Pos.CENTER);
        libraryHBox.setLayoutX(1.0);
        libraryHBox.setLayoutY(218.0);
        libraryHBox.setPrefHeight(36.0);
        libraryHBox.setPrefWidth(231.0);
        libraryHBox.setSpacing(10.0);

        // 图书馆系统图标
        ImageView libraryIcon = new ImageView();
        try {
            Image libraryImage = new Image("library.png");
            libraryIcon.setImage(libraryImage);
        } catch (Exception e) {
            System.out.println("无法加载图书馆系统图标: " + e.getMessage());
        }
        libraryIcon.setFitHeight(58.0);
        libraryIcon.setFitWidth(66.0);
        libraryIcon.setPreserveRatio(true);
        libraryIcon.setPickOnBounds(true);

        // 图书馆系统按钮
        libraryBtn.setAlignment(Pos.CENTER);
        libraryBtn.setMnemonicParsing(false);
        libraryBtn.setPrefHeight(42.0);
        libraryBtn.setPrefWidth(135.0);
        libraryBtn.setStyle("-fx-background-color: #1E90FF;");
        libraryBtn.setTextFill(Color.WHITE);
        libraryBtn.setFont(Font.font("System", FontWeight.BOLD, 18.0));

        libraryHBox.getChildren().addAll(libraryIcon, libraryBtn);

        // 创建商店系统HBox
        HBox storeHBox = new HBox();
        storeHBox.setAlignment(Pos.CENTER);
        storeHBox.setLayoutX(1.0);
        storeHBox.setLayoutY(281.0);
        storeHBox.setPrefHeight(48.0);
        storeHBox.setPrefWidth(231.0);
        storeHBox.setSpacing(10.0);

        // 商店系统图标
        ImageView storeIcon = new ImageView();
        try {
            Image storeImage = new Image("shop.png");
            storeIcon.setImage(storeImage);
        } catch (Exception e) {
            System.out.println("无法加载商店系统图标: " + e.getMessage());
        }
        storeIcon.setFitHeight(58.0);
        storeIcon.setFitWidth(66.0);
        storeIcon.setPreserveRatio(true);
        storeIcon.setPickOnBounds(true);

        // 商店系统按钮
        storeBtn.setAlignment(Pos.CENTER);
        storeBtn.setMnemonicParsing(false);
        storeBtn.setPrefHeight(42.0);
        storeBtn.setPrefWidth(135.0);
        storeBtn.setStyle("-fx-background-color: #1E90FF;");
        storeBtn.setTextFill(Color.WHITE);
        storeBtn.setFont(Font.font("System", FontWeight.BOLD, 18.0));

        storeHBox.getChildren().addAll(storeIcon, storeBtn);

        // 创建校徽图标
        ImageView schoolBadgeIcon = new ImageView();
        try {
            Image schoolBadgeImage = new Image("badge.png");
            schoolBadgeIcon.setImage(schoolBadgeImage);
        } catch (Exception e) {
            System.out.println("无法加载校徽图标: " + e.getMessage());
        }
        schoolBadgeIcon.setFitHeight(58.0);
        schoolBadgeIcon.setFitWidth(51.0);
        schoolBadgeIcon.setLayoutX(14.0);
        schoolBadgeIcon.setLayoutY(9.0);
        schoolBadgeIcon.setPreserveRatio(true);
        schoolBadgeIcon.setPickOnBounds(true);

        // 将所有组件添加到AnchorPane
        anchorPane.getChildren().addAll(
                backgroundImage, leftRect, topRect, titleText,
                changePasswordBtn, logoutBtn, bankHBox, studentInfoHBox,
                courseSelectionHBox, libraryHBox, storeHBox, schoolBadgeIcon
        );

        // 将AnchorPane添加到主VBox
        mainVBox.getChildren().add(anchorPane);

        // 创建场景并显示
        Scene scene = new Scene(mainVBox);
        primaryStage.setTitle("虚拟校园系统 - 学生版");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public void setChangePasswordButtonAction(java.util.function.Consumer<javafx.event.ActionEvent> handler) {
        changePasswordBtn.setOnAction(handler::accept);
    }

    public void setLogoutButtonAction(java.util.function.Consumer<javafx.event.ActionEvent> handler) {
        logoutBtn.setOnAction(handler::accept);
    }

    public void setCourseSelectionButtonAction(java.util.function.Consumer<javafx.event.ActionEvent> handler) {
        courseSelectionBtn.setOnAction(handler::accept);
    }

    public void setStoreButtonAction(java.util.function.Consumer<javafx.event.ActionEvent> handler) {
        storeBtn.setOnAction(handler::accept);
    }

    public void setLibraryButtonAction(java.util.function.Consumer<javafx.event.ActionEvent> handler) {
        libraryBtn.setOnAction(handler::accept);
    }

    public void setBankButtonAction(java.util.function.Consumer<javafx.event.ActionEvent> handler) {
        bankBtn.setOnAction(handler::accept);
    }

    public void setStudentInfoButtonAction(java.util.function.Consumer<javafx.event.ActionEvent> handler) {
        studentInfoBtn.setOnAction(handler::accept);
    }

    public void showLoginFrame() throws IOException {
        // 获取当前登录窗口的Stage
        Stage registerStage = (Stage) logoutBtn.getScene().getWindow();

        // 创建主界面
        LoginFrame loginFrame = new LoginFrame();
        Stage loginStage = new Stage();

        // 显示主界面
        loginFrame.start(loginStage);

        // 关闭登录窗口
        registerStage.close();
    }

    public void showPasswordChangeFrame() throws IOException {
        // 获取当前登录窗口的Stage
        Stage registerStage = (Stage) changePasswordBtn.getScene().getWindow();

        // 创建主界面
        PasswordChangeFrame passwordChangeFrame = new PasswordChangeFrame();
        passwordChangeFrame.setUsername(user.getId());
        passwordChangeFrame.setPassword(user.getPassword());
        passwordChangeFrame.setRole("学生");
        Stage passwordChangeStage = new Stage();

        // 显示主界面
        passwordChangeFrame.start(passwordChangeStage);

        // 关闭登录窗口
        registerStage.close();
    }

    public void showStudentCourseFrame() throws Exception {
        // 获取当前登录窗口的Stage
        Stage registerStage = (Stage) courseSelectionBtn.getScene().getWindow();

        // 创建主界面
        StudentCourseFrame studentCourseFrame = new StudentCourseFrame();
        studentCourseFrame.setUser(user.getId(), user.getPassword(),  user.getRole());
        Stage studentCourseStage = new Stage();

        // 显示主界面
        studentCourseFrame.start(studentCourseStage);

        // 关闭登录窗口
        registerStage.close();
    }

    public void showShopFrame() throws Exception {
        // 获取当前登录窗口的Stage
        Stage registerStage = (Stage) storeBtn.getScene().getWindow();

        // 创建主界面
        ShopFrame shopFrame = new ShopFrame();
        shopFrame.setUser(user.getId(), user.getPassword(),  user.getRole());
        Stage shopStage = new Stage();

        // 显示主界面
        shopFrame.start(shopStage);

        // 关闭登录窗口
        registerStage.close();
    }

    public void showLibraryFrame() throws Exception {
        // 获取当前登录窗口的Stage
        Stage registerStage = (Stage) libraryBtn.getScene().getWindow();

        // 创建主界面
        LibraryFrame libraryFrame = new LibraryFrame();
        libraryFrame.setUser(user.getId(), user.getPassword(),  user.getRole());
        Stage libraryStage = new Stage();

        // 显示主界面
        libraryFrame.start(libraryStage);

        // 关闭登录窗口
        registerStage.close();
    }

    public void showBankFrame() throws Exception {
        // 获取当前登录窗口的Stage
        Stage registerStage = (Stage) bankBtn.getScene().getWindow();

        // 创建主界面
        BankFrame bankFrame = new BankFrame();
        bankFrame.setUser(user.getId(), user.getPassword(),  user.getRole());
        Stage bankStage = new Stage();

        // 显示主界面
        bankFrame.start(bankStage);

        // 关闭登录窗口
        registerStage.close();
    }

    public void showStudentInfoFrame() throws Exception {
        // 获取当前登录窗口的Stage
        Stage registerStage = (Stage) studentInfoBtn.getScene().getWindow();

        // 创建主界面
        StudentInfoFrame studentInfoFrame = new StudentInfoFrame();
        studentInfoFrame.setUser(user.getId(), user.getPassword(),  user.getRole());
        Student student=connector.getStudentById(user.getId()).getStudent();
        studentInfoFrame.setStudent(student.getStudentId(),student.getStudentName(),student.getAcademicRecord());
        Stage studentInfoStage = new Stage();

        // 显示主界面
        studentInfoFrame.start(studentInfoStage);

        // 关闭登录窗口
        registerStage.close();
    }

}